package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.appdetail.pojo.dto.PhoneForDialGetDTO;
import com.xbongbong.pro.appdetail.pojo.dto.ReadMessageSetDTO;
import com.xbongbong.pro.appdetail.pojo.dto.SingleDataMessageGetDTO;
import com.xbongbong.pro.appdetail.pojo.vo.PhoneForDialGetVO;
import com.xbongbong.pro.appdetail.pojo.vo.SingleDataMessageGetVO;
import com.xbongbong.pro.applist.pojo.dto.MobileDetailThroughDTO;
import com.xbongbong.pro.businessdata.pojo.dto.FocusDTO;
import com.xbongbong.pro.businesstage.pojo.vo.StageLogListVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.customer.pojo.dto.ChangeStatusDTO;
import com.xbongbong.pro.detailtab.dto.DelFromMyTeamDTO;
import com.xbongbong.pro.detailtab.dto.DelParentOrChildCustomerDTO;
import com.xbongbong.pro.detailtab.dto.DetailTabDataGetDTO;
import com.xbongbong.pro.detailtab.dto.MyTeamGetDTO;
import com.xbongbong.pro.detailtab.dto.SetChildCustomerDTO;
import com.xbongbong.pro.detailtab.dto.SetParentCustomerDTO;
import com.xbongbong.pro.detailtab.dto.UpdateMyTeamDTO;
import com.xbongbong.pro.detailtab.vo.MyTeamGetVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.through.pojo.vo.ResultThroughVO;
import com.xbongbong.saas.service.ListBatchService;
import com.xbongbong.saas.service.MobileDetailService;
import com.xbongbong.saas.service.SaasDetailTabService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 移动端详情页特殊接口
 *
 * @author long.rao
 * @version v1.0
 * @date 2019/4/15 10:24
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile")
public class MobileSaasSpecialController {
    @Resource
    private ListBatchService listBatchService;
    @Resource
    private SaasDetailTabService saasDetailTabService;
    @Resource
    private MobileDetailService mobileDetailService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    /**
     * 修改状态
     *
     * @param changeStatusDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-04 13:59
     */
    @RequestMapping(value = "/customer/changeStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeCustomerStatus(@RequestBody @Valid ChangeStatusDTO changeStatusDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(changeStatusDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(changeStatusDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }

                BaseVO baseVO = saasDetailTabService.changeStatus(changeStatusDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 关注
     * @param focusDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/focus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String focus(@RequestBody @Valid FocusDTO focusDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = listBatchService.focus(focusDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取团队
     *
     * @param myTeamGetDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-15 15:33
     */
    @RequestMapping(value = "/myTeam/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMyTeam(@RequestBody @Valid MyTeamGetDTO myTeamGetDTO, BindingResult br) {
        XbbResponse<MyTeamGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MyTeamGetVO myTeamGetVO = saasDetailTabService.getMyTeam(myTeamGetDTO);
                response = new XbbResponse<>(myTeamGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 加入团队
     *
     * @param updateMyTeamDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-15 15:34
     */
    @RequestMapping(value = "/addToMyTeam", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addToMyTeam(@RequestBody @Valid UpdateMyTeamDTO updateMyTeamDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = saasDetailTabService.updateMyTeam(updateMyTeamDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 移除协同团队
     *
     * @param delFromMyTeamDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-15 15:35
     */
    @RequestMapping(value = "/delFromMyTeam", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delFromMyTeam(@RequestBody @Valid DelFromMyTeamDTO delFromMyTeamDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = saasDetailTabService.delFromMyTeam(delFromMyTeamDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除上下级客户
     *
     * @param delParentOrChildCustomerDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-15 19:19
     */
    @RequestMapping(value = "/parentOrChildCustomer/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delParentOrChildCustomer(@RequestBody @Valid DelParentOrChildCustomerDTO delParentOrChildCustomerDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = saasDetailTabService.delParentOrChildCustomer(delParentOrChildCustomerDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置上级客户
     *
     * @param setParentCustomerDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-15 19:20
     */
    @RequestMapping(value = "/parentCustomer/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setParentCustomer(@RequestBody @Valid SetParentCustomerDTO setParentCustomerDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = saasDetailTabService.setParentCustomer(setParentCustomerDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置下级客户
     *
     * @param setChildCustomerDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-15 19:20
     */
    @RequestMapping(value = "/childCustomer/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setChildCustomer(@RequestBody @Valid SetChildCustomerDTO setChildCustomerDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = saasDetailTabService.setChildCustomer(setChildCustomerDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 阶段列表
     *
     * @param detailTabDataGetDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-04-17 15:56
     */
    @RequestMapping(value = "/getBusinessStageArray", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBusinessStageArray(@RequestBody @Valid DetailTabDataGetDTO detailTabDataGetDTO, BindingResult br) {
        XbbResponse<StageLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageLogListVO stageLogListVO = saasDetailTabService.getCustomerStageArray(detailTabDataGetDTO);
                response = new XbbResponse<>(stageLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取移动端拨号联系人底部栏
     *
     * @param phoneForDialGetDTO 入参
     * @param br 错误
     * @author feng.zheng
     * @date 2019-04-15 19:20
     */
    @RequestMapping(value = "/phoneForDial/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPhoneForDial(@RequestBody @Valid PhoneForDialGetDTO phoneForDialGetDTO, BindingResult br) {
        XbbResponse<PhoneForDialGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PhoneForDialGetVO phoneForDialGetVO = mobileDetailService.getPhoneForDial(phoneForDialGetDTO);
                response = new XbbResponse<>(phoneForDialGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/singleDataMessage/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSingleDataMessage(@RequestBody @Valid SingleDataMessageGetDTO singleDataMessageGetDTO, BindingResult br) {
        XbbResponse<SingleDataMessageGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SingleDataMessageGetVO singleDataMessageGetVO = mobileDetailService.getSingleDataMessage(singleDataMessageGetDTO);
                response = new XbbResponse<>(singleDataMessageGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/readMessage/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setReadMessage(@RequestBody @Valid ReadMessageSetDTO readMessageSetDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = mobileDetailService.setReadMessage(readMessageSetDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 移动端详情页穿透
     *
     * @param mobileDetailThroughDTO
     * @param br
     * @throws
     * @author long.rao
     * @date 2019-05-29 14:47
     */
    @RequestMapping(value = "/getDetailThrough", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDetailThrough(@RequestBody @Valid MobileDetailThroughDTO mobileDetailThroughDTO, BindingResult br) {
        XbbResponse<ResultThroughVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ResultThroughVO resultThroughVO = mobileDetailService.getDetailThrough(mobileDetailThroughDTO);
                response = new XbbResponse<>(resultThroughVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
